package com.ying.dogying.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ying.dogying.entity.ImCategory;
import com.ying.dogying.service.ImCategoryService;
import com.ying.dogying.service.ImPostService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.List;

@Component
public class ContextStartup implements ApplicationRunner, ServletContextAware {
	private static Logger log = LoggerFactory.getLogger(ContextStartup.class);
    ServletContext servletContext;

    @Autowired
    private ImPostService imPostService;

    @Autowired
    private ImCategoryService imCategoryService;

    /**
     * 在启动之后执行该方法
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        servletContext.setAttribute("base", servletContext.getContextPath());
        // 启动时调用初始化周排名
        imPostService.initIndexWeekRank();
        log.info("ContextStartup---------------->加载本周热议");

        List<ImCategory> categories = imCategoryService.list(new QueryWrapper<ImCategory>()
                .eq("status", 0));
        servletContext.setAttribute("categories", categories);
        log.info("ContextStartup---------------->加载分类列表");

    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;

    }

}